//
//  ViewController.m
//  BluetoothBattle
//
//  Created by LuckyStrike on 2017/5/22.
//  Copyright © 2017年 LuckyStrike. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<CBPeripheralManagerDelegate>

/** CBPeripheralManager  */
@property (nonatomic,strong) CBPeripheralManager *peripheralMgr;
/** CBMutableCharacteristic  */
@property (nonatomic,strong) CBMutableCharacteristic *transferCharacteristic;

/**   */
@property (nonatomic,strong) CBCentral *targetCentral;
@property (strong, nonatomic) IBOutlet UILabel *receiveLab;
@property (strong, nonatomic) IBOutlet UITextView *sendInfoView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}

- (void)appServer{
    
    _peripheralMgr = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    
    
}

- (void)sendMessage{
    NSData *data = [self.sendInfoView.text dataUsingEncoding:NSUTF8StringEncoding];
    
    [self.peripheralMgr updateValue:data forCharacteristic:self.transferCharacteristic onSubscribedCentrals:@[self.targetCentral]];
}

- (void)setup{
    CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:SERVICE_UUID] primary:YES];
    
    self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:CHARACTERISTIC_UUID_SEND] properties:CBCharacteristicPropertyNotify|CBCharacteristicPropertyWrite|CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadable|CBAttributePermissionsWriteable];
    //建立关系
    transferService.characteristics = @[self.transferCharacteristic];
    [self.peripheralMgr addService:transferService];
    //开始广播
    [self.peripheralMgr startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:SERVICE_UUID]] }];
}

#pragma mark -- CBPeripheralManagerDelegate

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    NSLog(@"ViewController state%ld",(long)peripheral.state);
    switch (peripheral.state) {
        case CBManagerStatePoweredOn:
            self.receiveLab.text = @"CBManagerStatePoweredOn";
            break;
            
        default:
            break;
    }
    
    
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error{
    
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{
    self.targetCentral = central;
    _receiveLab.text = @"已被订阅";
}

//收到数据
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request{
    _receiveLab.text = [[NSString alloc] initWithData:request.value encoding:NSUTF8StringEncoding];
}

- (IBAction)sendNotification:(id)sender {
    [self appServer];
}

- (IBAction)sendInfo:(id)sender {
    [self sendMessage];
}

@end
